/**
 * 2017/7/6.
 * 公共类
 */
var cncbk_common_tools = {
    //服务器域名
     'serverURL': 'http://ww.wuyoutianxia.com/wy3pt',
     // 'serverURL': 'http://test.wuyoutianxia.com/wy3pt',
    // 'serverURL': 'http://192.168.0.118/watw',
	// 'serverURL': 'http://www1.wuyoutianxia.com/wy3pt/',
    //web项目名
    'projectName': '/admin',
    //图片URL
    'imgURL': 'http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'

}

//后台服务地址
var serverURL = cncbk_common_tools.serverURL;

//项目名
var projectName = cncbk_common_tools.projectName;

//合并服务器地址与项目名
serverURL += projectName;

//初始化当前页//
var page = 1;

//一页的条数
var pagesize = 10;
console.log($.cookie("navData"))

//导航信息
if ($.cookie("navData")) {
    var navData = $.cookie("navData")
    var navListData = JSON.parse(navData)
	console.log(navListData)
} else {
    window.location.href = "login.html";
}
//ajax
function ajax(url, data,successFunc,type, dataType) {
	var type = type || 'post';//请求类型
	var dataType = dataType || 'json';//接收数据类型
	var success = success || function (res) {
		 var res = res;
		 console.log(res);
		 layer.closeAll('loading');
		 layer.msg(res.msg)
			if(res.code == 200){
				successFunc(res)
			}else if (res.code == 400) {
			  	layer.msg('登陆信息错误，请重新登陆');
			  	window.location.href = "login.html";
		  	}else if(res.code == 555){
				layer.msg('请求失败,没有权限访问');
				window.location.href = "login.html";
			}
		};
	var error = error || function (res) {
			/*res.status;//错误状态吗*/
			layer.closeAll('loading');
			setTimeout(function () {
				if(res.code == 404){
					layer.msg('请求失败，请求未找到');
				}else if (res.code == 400) {
			  		layer.msg('登陆信息错误，请重新登陆');
			  		window.location.href = "login.html";
				}else if(res.code == 555){
					layer.msg('请求失败,没有权限访问');
			  		window.location.href = "login.html";
				}else {
					layer.msg('请求失败,网络连接超时');
				}
			},500);
		};
	$.ajax({
		'url': url,
		'data': data,
		'type': type,
		'dataType': dataType,
		'success': success,
		'error': error,
		'beforeSend': function () {
			layer.load(2);
		},
	});
}
// console.log(navListData);

//时间戳转换
function getMyDate(str) {
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen); //最后拼接时间
    return oTime;
};
//补0操作
function getzf(num) {
    if (parseInt(num) < 10) {
        num = '0' + num;
    }
    return num;
}

//获取当前年月日
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

//退出登陆.
$("#loginOut").click(function () {
    $.ajax({
        type: 'post',
        data: null,
        url: serverURL + '/loginOut',
        dataType: 'json',
        success: function (data) {
            console.log(data)
            if (data.code == 400) {
                alert("登陆超时请重新登陆")
                window.location.href = "login.html";
            } else if (data.code == 200) {
                layer.msg("退出登陆成功");
                window.location.href = "login.html";
            }
        }
    })
})

//图片url
function getObjectURL(fis) {
	var urls = null;
	// 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
	// basic
	if (window.createObjectURL != undefined) {
		urls = window.createObjectURL(fis);
		// mozilla(firefox)
	} else if (window.URL != undefined) {
		urls = window.URL.createObjectURL(fis);
		// webkit or chrome
	} else if (window.webkitURL != undefined) {
		urls = window.webkitURL.createObjectURL(fis);
	}
	return urls;
}

//权限导航
function navFunc() {
	var roleId = navListData.roleId
	var data = {
		'roleId':roleId
	}
	var cbk = function(res){
		console.log(navListData)
	    var navListDatas = res.data
	    $('#nav').empty();
	    $('#uname').html(navListData.adminName)
	    var navs = '';
	    for (var i = 0; i < navListDatas.length; i++) {
	        var list = navListDatas[i].list;
	        navs +=
	            '<li class="dropdown">' +
	            '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa ' + navListDatas[i].limitIcon + '"></i> ' + navListDatas[i].limitName + '<span class="caret"></span></a>' +
	            '<ul class="dropdown-menu" role="menu">'
	        	for (var k in list) {
	            navs += '<li><a class="navlist" target="_blank" href="' + list[k].limitUrl + '">' + list[k].limitName +
	                '</a></li>'
	        }
	        navs += '</ul>' +
	            '</li>'
	    }
	    $("#nav").append(navs)
	    active()
	}
	ajax(
		serverURL + '/platform/getLimitListByRoleId',
		data,
		cbk,
		'post',
		'json'
	)
}
navFunc()

//导航高亮
function active() {
    var current = $(".breadcrumb").children(".active").text()
    $('.navlist').each(function () {
        var _this = $(this)
        var navCurrent = _this.text();
        if (current == navCurrent) {
            _this.parent().parent().parent().addClass('active')
        }
    });
    console.log(current)
}

//搜索条件重置
$('#Reset').click(function () {
    $('.searchForm input').val(null);
})

//获取url参数
function getQueryString(key){
	var reg = new RegExp("(^|&)"+key+"=([^&]*)(&|$)");
	var result = window.location.search.substr(1).match(reg);
	return result?decodeURIComponent(result[2]):null;
}

//查看大图
$(document).on('click', '.imgsBox img', function() {
	// console.log($(this).parent().html())
	 layer.open({
		type: 1,
	    title: false,
	    closeBtn: 0,
	    area: 'aotu',
	    skin: 'layui-layer-nobg', //没有背景色
	    shadeClose: true,
	  	content: $(this).parent().html(),
	});
});
